package com.edu.medicalpro.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.edu.medicalpro.R;
import com.edu.medicalpro.bean.DoctorData;
import com.edu.medicalpro.bean.PatientResult;
import com.edu.medicalpro.bean.User;
import com.edu.medicalpro.mvp.HomeContrace;
import com.edu.medicalpro.mvp.presenter.HomePresenter;

        public class LoginActivity extends AppCompatActivity  implements HomeContrace.IView{
            private EditText usernameEditText, passwordEditText;
            private Button loginButton;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_login);
                HomePresenter homePresenter = new HomePresenter(this);
                usernameEditText = findViewById(R.id.etUsername);
                passwordEditText = findViewById(R.id.etPassword);
                loginButton = findViewById(R.id.btnLogin);
                loginButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String username = usernameEditText.getText().toString();
                        String password = passwordEditText.getText().toString();
                        // 进行用户名和密码验证
                        if (validateAccount(username, password)) {
                            // 账号通过验证，发送网络请求
                            // homePresenter .login("15213455348","123456");
                            homePresenter.login(username,password);
                            // 登录成功，跳转到主页
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();

                        } else {
                            // 账号验证失败，弹出提示框
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
            @Override
            public void loginResult(User user) {

                if(user.getCode()==200){
                    Toast.makeText(LoginActivity.this, user.getMessage(), Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(LoginActivity.this, user.getMessage(), Toast.LENGTH_SHORT).show();
                }
                Log.e("111",user.toString());



            }

            @Override
            public void LoginError(Throwable throwable) {

            }

            @Override
            public void Result(PatientResult user) {

            }

            @Override
            public void DoctorResult(DoctorData doctor) {

            }

            private boolean validateAccount(String username, String password) {
                // 这里可以对输入的用户名和密码进行校验，例如判断用户名和密码都不为空等条件
                return !TextUtils.isEmpty(username) && !TextUtils.isEmpty(password);
            }
        }